home *** CD-ROM | disk | FTP | other *** search
/ Team Palmtops 7 / Palmtops_numero07.iso / WinCE / SDKWindowsCE / HandHeldPCPro30 / sdk.exe / Jupiter SDK / data1.cab / MFC_Samples / palette / dibdoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-02-19  |  3.8 KB  |  180 lines

  1. // dibdoc.cpp : implementation of the CDibDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1999 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include <limits.h>
  15. #include "resource.h"
  16.  
  17. #include "dibdoc.h"
  18. #include "mainfrm.h"
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CDibDoc
  27.  
  28. IMPLEMENT_DYNCREATE(CDibDoc, CDocument)
  29.  
  30. BEGIN_MESSAGE_MAP(CDibDoc, CDocument)
  31.     //{{AFX_MSG_MAP(CDibDoc)
  32.     //}}AFX_MSG_MAP
  33. END_MESSAGE_MAP()
  34.  
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CDibDoc construction/destruction
  37.  
  38. CDibDoc::CDibDoc()
  39. {
  40.     m_hDIB = NULL;
  41.     m_palDIB = NULL;
  42.     m_sizeDoc = CSize(1,1);     // dummy value to make CScrollView happy
  43. }
  44.  
  45. CDibDoc::~CDibDoc()
  46. {
  47.     if (m_hDIB != NULL)
  48.     {
  49.         ::GlobalFree((HGLOBAL) m_hDIB);
  50.     }
  51.     if (m_palDIB != NULL)
  52.     {
  53.         delete m_palDIB;
  54.     }
  55. }
  56.  
  57. void CDibDoc::InitDIBData()
  58. {
  59.     if (m_palDIB != NULL)
  60.     {
  61.         delete m_palDIB;
  62.         m_palDIB = NULL;
  63.     }
  64.     if (m_hDIB == NULL)
  65.     {
  66.         return;
  67.     }
  68.     // Set up document size
  69.     LPSTR lpDIB = (LPSTR)m_hDIB;
  70.     if (::DIBWidth(lpDIB) > INT_MAX ||::DIBHeight(lpDIB) > INT_MAX)
  71.     {
  72.         ::GlobalFree((HGLOBAL) m_hDIB);
  73.         m_hDIB = NULL;
  74.         CString strMsg;
  75.         MessageBox(NULL, CString((LPCTSTR)IDS_IMAGE), NULL, MB_ICONINFORMATION | MB_OK);
  76.         return;
  77.     }
  78.     m_sizeDoc = CSize((int) ::DIBWidth(lpDIB), (int) ::DIBHeight(lpDIB));
  79.     // Create copy of palette
  80.     m_palDIB = new CPalette;
  81.     if (m_palDIB == NULL)
  82.     {
  83.         // we must be really low on memory
  84.         ::GlobalFree((HGLOBAL) m_hDIB);
  85.         m_hDIB = NULL;
  86.         return;
  87.     }
  88.     if (::CreateDIBPalette(m_hDIB, m_palDIB) == NULL)
  89.     {
  90.         // DIB may not have a palette
  91.         delete m_palDIB;
  92.         m_palDIB = NULL;
  93.         return;
  94.     }
  95. }
  96.  
  97.  
  98. BOOL CDibDoc::OnOpenFile(LPCTSTR lpszPathName)
  99. {
  100.     CFile file;
  101.     CFileException fe;
  102.  
  103. #if !defined(READ_BM_FROM_RESOURCE)
  104.     if (!file.Open(lpszPathName, CFile::modeRead | CFile::shareDenyWrite, &fe))
  105.     {
  106.         ReportSaveLoadException(lpszPathName, &fe,
  107.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  108.         return FALSE;
  109.     }
  110. #endif
  111.  
  112.     DeleteContents();
  113.     BeginWaitCursor();
  114.  
  115.     // replace calls to Serialize with ReadDIBFile function
  116.     TRY
  117.     {
  118.         m_hDIB = ::ReadDIBFile(file);
  119.     }
  120.     CATCH (CFileException, eLoad)
  121.     {
  122.         file.Abort(); // will not throw an exception
  123.         EndWaitCursor();
  124.         ReportSaveLoadException(lpszPathName, eLoad,
  125.             FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
  126.         m_hDIB = NULL;
  127.         return FALSE;
  128.     }
  129.     END_CATCH
  130.  
  131.     InitDIBData();
  132.     EndWaitCursor();
  133.  
  134.     if (m_hDIB == NULL)
  135.     {
  136.         // may not be DIB format
  137.         CString strMsg;
  138.         MessageBox(NULL, CString((LPCTSTR)IDS_LOAD), NULL, MB_ICONINFORMATION | MB_OK);
  139.         return FALSE;
  140.     }
  141.     SetPathName(lpszPathName);
  142.     SetModifiedFlag(FALSE);     // start off with unmodified
  143.  
  144.     POSITION pos = GetFirstViewPosition();
  145.     CView* pView = GetNextView( pos );
  146.     if(pView)
  147.         pView->SendMessage(WM_USER, (WPARAM)pView->m_hWnd);
  148.  
  149.     return TRUE;
  150. }
  151.  
  152. void CDibDoc::ReplaceHDIB(HDIB hDIB)
  153. {
  154.     if (m_hDIB != NULL)
  155.     {
  156.         ::GlobalFree((HGLOBAL) m_hDIB);
  157.     }
  158.     m_hDIB = hDIB;
  159. }
  160.  
  161.  
  162. /////////////////////////////////////////////////////////////////////////////
  163. // CDibDoc diagnostics
  164.  
  165. #ifdef _DEBUG
  166. void CDibDoc::AssertValid() const
  167. {
  168.     CDocument::AssertValid();
  169. }
  170.  
  171. void CDibDoc::Dump(CDumpContext& dc) const
  172. {
  173.     CDocument::Dump(dc);
  174. }
  175.  
  176. #endif //_DEBUG
  177.  
  178. /////////////////////////////////////////////////////////////////////////////
  179. // CDibDoc commands
  180.